<template>
  <view class="content-box">
    <!-- 筛选项 -->
    <orderHeader
      @changeTradeType="changeTradeType"
      @changeTradeScene="changeTradeScene"
			@onChangeStatus="onChangeStatus"
			@changeData="changeData"
			@resetTime="resetTime"
      @back="leftClick(urlIndex)"
      :tradeType="tradeType"
      :tradeScene="tradeScene"
			:oderStatus="tradeType == 0 ? oderSellStatus : oderpurchaseStatus"
			:defaultTimeArr="tradeScene == 1 ? orderTimeArr : orderExchangeTimeArr"
    ></orderHeader>
    <!-- 列表 -->
    <sell-list
      :orderList="orderList"
      v-if="tradeType == 0"
      @refresh="refresh"
      :tradeType="tradeType"
      :tradeScene="tradeScene"
      :currencyOption="currencyOption"
    ></sell-list>
    <purchase-list
      :orderList="orderList"
      v-if="tradeType == 1"
      @refresh="refresh"
      :tradeType="tradeType"
      :tradeScene="tradeScene"
      :currencyOption="currencyOption"
    ></purchase-list>
    <u-empty
      v-if="orderList.length == 0"
      :text="noneOrderTips"
      textColor="#878787"
      textSize="14"
      width="104"
      height="100"
      marginTop="160"
      icon="/static/image/bourse/none_order.png"
    >
    </u-empty>
  </view>
</template>

<script>
import orderHeader from "./components/orderHeader.vue";
import { orderStatusText } from "@/utils/common";
import OrderExchange from "./components/orderExchange.vue";
import {
  getUserOrderList,
  submitsCertificateA,
  getTradeList,
  getCurrencyList,
} from "@/services/bourse";
import { mapActions, mapState } from "vuex";
import PurchaseList from "./components/purchaseList.vue";
import SellList from "./components/sellList.vue";
export default {
  components: { orderHeader, OrderExchange, PurchaseList, SellList },
  data() {
    return {
      tradeType: 0, //操作类型：0 卖出/1买入
      tradeScene: 1, //交易场景: OTC场 1; 0L场内 2'
      orderList: [],
      pageSize: 10,
      pageNum: 1,
      total: 0,
      tradeTotal: 0,
      currencyOption: [],
			oderSellStatus: -1,
			oderpurchaseStatus: -1,
			otherOderParmas: {},
			otherExchangeOderParmas:{},
			orderTimeArr: [],
			orderExchangeTimeArr: [],
      noneOrderTips: this.$t('order.No-order-yet'),
			urlIndex: "bourseOrder"
    };
  },
  computed: {
    ...mapState(["userInfo"]),
  },
  onLoad(options) {
		this.getPrePages(this.urlIndex) // 获取上一个页面的路径已经参数
    if (options.tradeScene) {
      this.tradeScene = options.tradeScene;
    }
    if (options.tradeType) {
      this.tradeType = options.tradeType;
    }
  },
  onShow() {
    this._getCurrencyList();
    this.refresh();
  },
  methods: {
    // 交易场景筛选
    changeTradeScene(tab) {
      this.tradeScene = tab;
      this.refresh();
    },
    // 操作类型筛选
    changeTradeType(tab) {
      this.tradeType = tab;
      this.refresh();
    },

    // 订单
    async getList() {
      const api = this.tradeType == 0 ? getTradeList : getUserOrderList;
			let options = {}
			if(this.tradeScene == 1) {
				options = {
				  pageSize: this.pageSize,
				  pageNum: this.pageNum,
				  userId: this.userInfo.userId,
				  orderByColumn: "createTime",
				  isAsc: "desc", //desc
				  tradeScene: this.tradeScene, //交易场景:OTC场1; 0L场内2
					...this.otherOderParmas
				
				}
			} else {
				options = {
				  pageSize: this.pageSize,
				  pageNum: this.pageNum,
				  userId: this.userInfo.userId,
				  orderByColumn: "createTime",
				  isAsc: "desc", //desc
				  tradeScene: this.tradeScene, //交易场景:OTC场1; 0L场内2
					...this.otherExchangeOderParmas
				
				}
				
			}
      // 场内交易订单只展示已完成的
      if (this.tradeScene == 2) {
        options.status = 20;
      } else {
        if (this.tradeType == 0) {
          // 卖出
          if (this.oderSellStatus !== -1) options.status = this.oderSellStatus;
        } else if (this.tradeType == 1) {
          // 买入
          if (this.oderpurchaseStatus !== -1)
            options.status = this.oderpurchaseStatus;
        }
        options.geStatus = 2;
      }

      const resp = await api({ ...options });
      uni.stopPullDownRefresh();
      if (!resp.success) return;
      let orderList;
      if (this.tradeType == 0) {
        orderList = resp.result.rows.map((item) => {
          return {
            ...item.exchangeOrderList[0],
            ...item,
            statusText : orderStatusText(item.status)
          };
        });
        console.log(orderList, 'orderList');
      } else {
        orderList = resp.result.rows.map((item) => {
          return {
           
            ...item.exchangeVo,
            ...item,
            statusText : orderStatusText(item.status)
          };
        });
      }
      this.total = resp.result.total;
      this.orderList =
        this.pageNum == 1 ? orderList : this.orderList.concat(orderList);
    },
    // 场外交易确认给乙方打款
    async confirmPayment(item) {
      const resp = await submitsCertificateA({
        orderId: item.orderId,
      });
      if (!resp.success) return;
      uni.showToast({
        title: this.$t("index.operation-success"),
        icon: "none",
        duration: 1500,
      });
      this.refresh();
    },
    refresh() {
      this.pageNum = 1;
      this.orderList = [];
      this.getList();
    },
    // 获取币种
    async _getCurrencyList() {
      const { success, result } = await getCurrencyList();
      if (success) {
        this.currencyOption = result.data;
      }
    },
		onChangeStatus(tradeScene, state) {
			if(this.tradeType == 0) { // 卖出
				this.oderSellStatus = state
			} else if(this.tradeType == 1) { // 买入
				this.oderpurchaseStatus = state
			}
			this.refresh();
		},
		changeData(timeVal, timeArr) {
			if(this.tradeScene == 1) { // OTC
			this.orderTimeArr = timeArr
				this.otherOderParmas = timeVal
			} else if(this.tradeScene == 2) { // 场内
				this.orderExchangeTimeArr = timeArr
				this.otherExchangeOderParmas = timeVal
			}
			console.log(this.otherOderParmas);
			this.refresh();
		},
		resetTime() {
			if(this.tradeScene == 1) { // OTC
				this.orderTimeArr = []
				this.otherOderParmas = {}
			} else if(this.tradeScene == 2) { // 场内
				this.orderExchangeTimeArr = []
				this.otherExchangeOderParmas = {}
			}
			this.refresh();
		}
  },
  onPullDownRefresh() {
    this.refresh();
  },
  onReachBottom() {
    if (this.total > this.pageNum * this.pageSize) {
      this.pageNum++;
      this.getList();
    }
  },
};
</script>

<style lang="scss" scoped>
.content-box {
  width: 100%;
  box-sizing: border-box;
  min-height: 100vh;
  padding-top: var(--status-bar-height);
}
</style>